/***************************************************************************
 *   Copyright (C) 2007 by Autostop team                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
///@file gnetserver.h Base class for TCP server

#ifndef GNETSERVER_H
#define GNETSERVER_H

// qt
#include <QtGlobal>
#include <QObject>
#include <QHostAddress>
#include <QTcpServer>

#if QT_VERSION >= 0x040300 && !defined QT_NO_OPENSSL
	#include <QSslSocket>
#else
	#include <QTcpSocket>
#endif

// local
class GNetPacket;
class GHeader;

/// Base class for GPacket-based tcp server. Attaches itself to QTcpSocket* instance and processes
/// incoming packets
/// Attach socket directly with setSocket() or indirectly, listening with listen().
/// Packets are processed in infinite loop in processPackets() method.
/// Implement createPacket(0 to recognize and create packet object basing on its header.
/// Implement processPacket() to handle incoming packets.
/// To send packet use sendPacket() method.
class GNetServer : public QObject
{
	Q_OBJECT
public:

	// construction / destruction

	explicit GNetServer( QObject* pParent = NULL );
	virtual ~GNetServer();

	// network
	
	void setSocket( QTcpSocket * pSocket );			///< Set socket
#if QT_VERSION >= 0x040300 && !defined QT_NO_OPENSSL
	void setSocket( QSslSocket * pSocket );			///< Set socket
#endif
		
	void setSocketDescriptor( int socketDescriptor,
							  QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState,
							  QIODevice::OpenMode openMode = QIODevice::ReadWrite
							);
	/// listen for incoming connection
	void listen( quint16 port, const QHostAddress & address = QHostAddress::Any );
	bool isConnected() const;						///< check conection state
	void disconnectFromHost();						///< break connection

	void setAuthorized( bool authorizated ) { _authorized = authorizated; }
	bool authorizated() const { return _authorized; }
	
	// packet processing
	void processPackets();							///< Packet processing loop

protected:
	/// sends packet
	virtual void sendPacket( const GNetPacket & packet );
	
	/// Waits for packet
	virtual GNetPacket* waitForPacket();

	/// Process incoming packet
	virtual void processPacket( GNetPacket * pPacket ) = 0;
	
	/// Starts an authorization loop
	void processAuthorization();
	virtual quint8 passUser( const QString & userName, const QString & password ) = 0;

private:

#if QT_VERSION >= 0x040300 && !defined QT_NO_OPENSSL
	QSslSocket * _pSocket;	///< comm socket
#else
	QTcpSocket * _pSocket;	///< comm socket
#endif
	
	QTcpServer	 _server;	///<server
	int			 _timeout;	///< aloowable timeout [ms]
	
	bool         _authorized;  ///< describes wheather client password pass
};

#endif // GNETSERVER_H

// eof


